//////////////////////////////////////////////////////
// Engine.h
//////////////////////////////////////////////////////

#pragma once

#include <windows.h>
#include <d3d8.h>
#include "Direct3D.h"
#include <time.h>
#include <fstream>
#include "constants.h"
#include "Player.h"
#include "Person.h"
#include "People.h"
#include "Door.h"
#include "Doors.h"
#include "Container.h"
#include "Containers.h"
#include "Monster.h"
#include "DirectSound.h"
#include "MyMonster.h"

// Define a custom vertex type and structure.
#define D3D8T_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_TEX1)
struct vertex
{
    float x, y, z, rhw;
    float tu,tv;
};

class CEngine
{
protected:
    HWND m_hWnd;
    CDirect3D m_Direct3D;
    CPlayer m_Player;
	CMyMonster *m_Monster;
	CMyMonster mon[MaxMonsters];
    CPeople m_People;
    CDoors m_Doors;
    CContainers m_Containers;
    BYTE m_byMapType;
    int m_iCurrentMap;
    char m_Sectors[MAPSECTORCOUNT];
	// for highlight and actor layers
	char a_Sectors[MAPSECTORCOUNT];
	char h_Sectors[MAPSECTORCOUNT];
	// - Cai 1/28/07 
    char m_pstrFirstMap[_MAX_PATH];
    int m_iFirstMapType;
    int m_iStartSector;
	// for draw start sector
	int m_iStartDrawSector;
	// - Cai 2/5/07
    int m_iAnimationTimerCount;
    BOOL m_bAnimationTilesUpdated;
    int m_bFastAnimationTilesUpdated;
	int m_bAnimateMovement; //int to store the movment value of an animated walking sequence 2-10-07
    BOOL m_bFastestAnimationTilesUpdated;
    BOOL m_bShowingPlaque;
    char m_pstrPlaqueMsg[1024];
    CMonster* m_pCurrentMonster;
    BOOL m_bPlayerTurnToAttack;
    BOOL m_bMonsterAttacking;
    BOOL m_bRunningFromMonster;
    int m_iCurrentMonsterFrame;
    BOOL m_iMonsterDamage;
    int m_iCombatDelayCounter;
    char m_pstrCurrentMapFile[MAX_PATH];
    BOOL m_bAskingIfSave;
    BOOL m_bAskingIfLoad;
    BOOL m_bAskingIfQuit;
    CDirectSound m_Sound;
    BOOL m_bPlayerDead;
    BOOL m_bGamePaused;
	bool m_bStartGame;
    BOOL m_bNoMonsters;
	int CurrentGameState; //11-30-06
	int CurrentEnemy;

public:
    CEngine(char* pstrMap, int iMapType, int iDrawSector, int iSector);
    ~CEngine(void);
    void SetWindowHandle(HWND hWnd);
    void ProcessGame();
    virtual void HandleKeys(WPARAM wParam);
    CDirect3D* GetCDirect3DObj();
    char* GetSectors();
    int GetCurrentMap();
    void SetCurrentMap(int map);
    void OpenMapFiles(char* pstrFileName, BOOL bNewGame);
    void InitGame();
    CPlayer* GetPlayer();
    void PaintBackBuffer();
	void DrawMovingDude(int DstCol, int DstRow, bool longrow); //moves the player by the variable m_bAnimateMovement
    HRESULT PlaceTile(IDirect3DSurface8* pBackBuffer,
        IDirect3DSurface8* pTileSurface, int TileNumber,
        int DstCol, int DstRow, int numTileCols, int numMapCols,
        int tileSize, int xOffset, int yOffset);
	int ColumnRow2Sector(int &newStart, bool &longrow, bool &newsctcount);
	/*
	original version
    int ColumnRow2Sector(int c, int r, int colCount, int);
	*/
    int TileNumber2SourceX(int TileNumber, int numCols, int tileSize);
    int TileNumber2SourceY(int TileNumber, int numCols, int tileSize);
    int Column2X(int col, int row, int tileSize, int numCols);
	int DudeColumn2X(int col, bool longrow, int tileSize, int numCols);
    int Row2Y(int r, int tileSize);
    int Sector2Column(int iSector, int numCols);
    int Sector2Row(int iSector, int numCols);
    void HandleUpArrowInMenu();
    void HandleDownArrowInMenu();
	void HandleEnterInMenu();
	void HandleUpArrow();
    void HandleDownArrow();
    void HandleLeftArrow();
    void HandleRightArrow();
	void HandleUpArrowBattleMenu();
	void HandleDownArrowBattleMenu();
	void HandleLeftArrowBattleMenu();
	void HandleRightArrowBattleMenu();
	void HandleEnter();
    void StartAnimationTimer();
    void HandleTimer();
    virtual void RespondToPlayerLocation() = 0;
    void ResetAnimation();
    BOOL IsShowingPlaque();
    void TurnOnPlaque(char* msg);
    void TurnOffPlaque();
    void PaintPlaque();
    CDirectSound* GetCDirectSoundObj();
    void SetPlayerDead();
	void SetGameState(int); //11-30-06
	int GetGameState(); //11-30-06
	void TitleScreenCleanUp(); //11-30-06


protected:
    virtual void HandleNPC(int iNPC);
    virtual BOOL HandleDoor(int iSector);
    virtual BOOL HandleContainer(int iSector);
    virtual BOOL IsBattleLocation() = 0;
    virtual CMonster* GetBattleMap() = 0;
    virtual void SaveGame(BOOL bFullSave) = 0;
    virtual void LoadGame() = 0;
    void SaveMapFile(BOOL bFullSave);
    void DeleteTempFiles();
    //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
    void DeleteSaveFiles();
    void CopyTempToSave();
        //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
    void CopySaveToTemp();
    void GetStringFromFile(std::ifstream& str, char* buf);
    virtual void HandleN();
    virtual void HandleY();
	void SetStartGame(bool);	//bool for title screen
	bool GetStartGame();	//bool for title screen

private:
    void ReadMapFile(char* FileName);
    void ConstructFileName(char* fileName, BOOL bNewGame);
    void ReadPeopleFile(char* fileName);
    void ReadContainerFile(char* fileName);
    void ReadDoorFile(char* fileName);
    void PaintBackground();
    void SetVisibleColsRows(int& LowVisibleCol, int& HiVisibleCol,
        int& LowVisibleRow, int& HiVisibleRow);
    void PaintPlayer(IDirect3DSurface8* pBackBuffer);
    void MovePlayerUp();
    void MovePlayerDown();
    void MovePlayerLeft();
    void MovePlayerRight();
    BOOL CharacterCanMove(int direction, int curSector);
    BOOL PlayerBlocked(int g_iPlayerSector, int direction);
    BOOL IsFastAnimationTile(int iSectorNum);
    BOOL IsFastestAnimationTile(int iTileNum);
    void HandleSlowAnimation();
    void HandleFastAnimation();
    void HandleFastestAnimation();
    int CheckForAndHandleDoor(int iSector);
    BOOL IsDoor(int iSector);
    CDoor* FindDoor(int iSector);
    BOOL CheckForAndHandleContainer(int iSector);
    BOOL ContainerEmpty(CContainer* pContainer);
    void RetrieveItemsFromContainer(CContainer* pContainer, char* msg);
    void RetrieveGold(CContainer* pContainer, char* msg);
    void RetrieveKeys(CContainer* pContainer, char* msg);
    void RetrievePotion(CContainer* pContainer, char* msg);
    void RetrieveWeapon(CContainer* pContainer, char* msg);
    void RetrieveArmor(CContainer* pContainer, char* msg);
    BOOL IsContainer(int iSector);
    CContainer* FindContainer(int iSector);
    void ShowInventory();
    int CheckForSecretDoor(int TileNumber, int iSector);
    BOOL SectorHasPerson(int iSector, int& person);
    void MoveNPCs();
    void MoveNPCNorth(int person);
    void MoveNPCEast(int person);
    void MoveNPCSouth(int person);
    void MoveNPCWest(int person);
    BOOL CheckForAndHandleNPC(int iSector);
    BOOL CheckForAndHandleBattle();
    void PaintTitleScreenCursor();
	void PaintTitleScreen();	//added 11-30-06
    void PaintBattlePlaque();
    void HandleMonsters();
    void AttackMonster();
    void RunFromMonster();
    BOOL CombatDelayOver();
    void AnimateIdleMonster();
    void AnimateMonsterDamage();
    void AttackRunningPlayer();
    void AttackPlayer();
    void TakePotion();
    void SavePeopleFile(char* pstrFileName);
    void SaveContainerFile(char* pstrFileName);
    void SaveDoorFile(char* pstrFileName);
    void ResetMap();
    void HandleS();
    void HandleL();
    void HandleSpace();
	void HandleEscapeBattleMenu();
    void HandleEscape();
    void EndGame();
    void HandleA();
	void HandleABattleMenu();
	void HandleMBattleMenu();
	void HandleSBattleMenu();
    void DisplayAttributes();
    void ResetDirect3D();

    void ToggleMonsters();
    void GetKeys();
    void GetGold();
    void GetExperience();
	void GetHitPoints();
	void move_highlights();
	void attack_highlights();
};
